﻿
Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Product_Preview
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'If Session("UserName") Is Nothing Then
        '    Dim retURL As String = Request.Path
        '    Response.Redirect("~/dang-nhap.html?retURL=" & retURL)
        'End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            Dim intProductId As Integer
            If Not Page.RouteData.Values("ProductId") Is Nothing Then
                intProductId = Page.RouteData.Values("ProductId")
                LoadData(intProductId)
            End If

        End If
    End Sub

    Private Sub LoadData(ByVal intProductId As Integer)
        Using Data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p = (From q In Data.Products Where q.Domain = DomainName And q.ProductId = intProductId _
                      Select q).FirstOrDefault
            If Not p Is Nothing Then
                ltrCode.Text = p.Code
                ltrTitle.Text = p.Title
                ltrPrice.Text = FormatNumber(p.OriginalPrice)
                ltrView.Text = p.Viewed.ToString
                If Not IsNothing(p.TinhTrangHang) Then
                    ltrStatus.Text = ArrTinhTrangHang(p.TinhTrangHang)
                Else
                    ltrStatus.Text = "Còn hàng"
                End If

                ltrXuatXu.Text = p.XuatXu
                ltrSoLuongCon.Text = (p.TongSo - p.DaBan).ToString
                ltrThongTinChung.Text = p.Description
                'Add DomainName DUC 09-09-2014
                Dim h = (From f In Data.ProductImages Where f.Domain = DomainName And f.ProductId = p.ProductId Select f).ToList
                rptBigBanner.DataSource = h
                rptBigBanner.DataBind()

                rptImageThumb.DataSource = h
                rptImageThumb.DataBind()
                'Thong tin nguoi ban
                lblHoTen.Text = IIf(IsNothing(p.HoTen) = True, "", p.HoTen)
                ltrDienThoai.Text = IIf(IsNothing(p.Phone) = True, "", p.Phone)
                ltrDiachi.Text = IIf(IsNothing(p.DiaChi) = True, "", p.DiaChi)

                ltrYahoo.Text = IIf(IsNothing(p.YahooId) = True, "", p.YahooId)
                ltrSky.Text = IIf(IsNothing(p.SkypeId) = True, "", p.SkypeId)
                ltrWebsite.Text = IIf(IsNothing(p.WebUrl) = True, "", p.WebUrl)

                'bind san pham cung cat
                'Add DomainName DUC 09-09-2014
                Dim k = (From q In Data.Products Where q.Domain = DomainName And q.ProductId <> p.ProductId And q.CateID = q.CateID And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
                             Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(15).ToList
                rptShow.DataSource = k
                rptShow.DataBind()


            End If
        End Using
    End Sub

    Protected Sub rptImageThumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImageThumb.ItemDataBound
        Dim row As ProductImage = e.Item.DataItem
        Dim imgThumb As Image = e.Item.FindControl("imgThumb")
        imgThumb.ImageUrl = CookedImageURL("90-60", row.ImageURL)
        imgThumb.AlternateText = row.Description
        imgThumb.Style.Add("overflow", "hidden")

    End Sub

    Protected Sub rptBigBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBigBanner.ItemDataBound
        Dim row As ProductImage = e.Item.DataItem
        Dim HighslideImage1 As Encosia.HighslideImage = e.Item.FindControl("HighslideImage1")
        HighslideImage1.ImageUrl = CookedImageURL("420-300", row.ImageURL)
        HighslideImage1.FullImageURL = CookedImageURL("800-600", row.ImageURL)
        HighslideImage1.AlternateText = row.Description
        If e.Item.ItemIndex = 0 Then
            HighslideImage1.Attributes.Add("style", "display:inline")
        Else
            HighslideImage1.Attributes.Add("style", "display:none")
        End If

    End Sub
    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptShow.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        Dim lblPrice As Label = CType(e.Item.FindControl("lblPrice"), Label)
        lblPrice.Text = FormatNumber(row.OriginalPrice.ToString) & " vnd"
        img.ImageUrl = CookedImageURL("80-80", row.ImagePreview.ToString)
        hplTitle.Text = row.Title
        hplImg.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
    End Sub

End Class
